import { _decorator, sys } from 'cc';
const { ccclass, property } = _decorator;

import { SaveData } from "./GlobalDefine";

@ccclass('SaveUtils')
export default class SaveUtils {
    //    /**
    //     * @zd 用户数据存储
    //     */
    //    //单例
    static readonly inst: SaveUtils = new SaveUtils();
    //    //读取本地数据
    getLocalData(CloudData?) {
        //        //SaveUtils.clear()
        //        // let userData = (CloudData!=""&&CloudData!=undefined)?CloudData:cc.sys.localStorage.getItem('Water_userData');
        let userData = sys.localStorage.getItem('Water_userData');
        //        //用户数据不为空
        if (userData != null && userData != "" && userData != "undefined") {
            userData = JSON.parse(userData);

            SaveData.GameData = userData.GameData != null ? userData.GameData : [[]];
            SaveData.GameDataProp = userData.GameDataProp != null ? userData.GameDataProp : [[0, 0, 0], [0, 0, 0]];
            SaveData.GameDataScore = userData.GameDataScore != null ? userData.GameDataScore : [0, 0];
            SaveData.isFirstEnter = userData.isFirstEnter != null ? userData.isFirstEnter : true //是否第一次进入
            SaveData.MaxScore = userData.MaxScore != null ? userData.MaxScore : [0, 0] //
            SaveData.MaxRank = userData.MaxRank != null ? userData.MaxRank : -1 //
            SaveData.isPropts = userData.isPropts != null ? userData.isPropts : true //
            SaveData.isADmax = userData.isADmax != null ? userData.isADmax : 0 //
            SaveData.LastUpDate = userData.LastUpDate != null ? userData.LastUpDate : new Date().getDate() //

            SaveData.isGuideOver = userData.isGuideOver != null ? userData.isGuideOver : false //
            SaveData.power = userData.power != null ? userData.power : 10  //体力
            SaveData.isGetPower = userData.isGetPower != null ? userData.isGetPower : false   //是否领取体力
            SaveData.nextPowerTime = userData.nextPowerTime != null ? userData.nextPowerTime : 0  //下次领取的时间戳
            SaveData.Volume = userData.Volume != null ? userData.Volume : 10 //初始音量
            SaveData.nextThreePower = userData.nextThreePower != null ? userData.nextThreePower : 0 //后3分钟刷新体力
            SaveData.shareCelue = userData.shareCelue != null ? userData.shareCelue : {
                strategy: [0, 0, 1, 1, 0, 1, 1, 1, 0, 1],
                num: 0,
                date: 0,
            }

            SaveData.ComboList = userData.ComboList != null ? userData.ComboList : [[], []]
            SaveData.ScoreList = userData.ScoreList != null ? userData.ScoreList : [[], []]
            SaveData.PaoPaoLevel = userData.PaoPaoLevel != null ? userData.PaoPaoLevel : 1

        }
        else {
            this.createNewUser();
        }
    }
    //    //创建新用户
    createNewUser() {
        console.log("创建新用户")
        SaveData.GameData = [[]]
        SaveData.GameDataScore = [0, 0]
        SaveData.GameDataProp = [[0, 0, 0], [0, 0, 0]]
        SaveData.isFirstEnter = true
        SaveData.MaxScore = [0, 0]
        SaveData.MaxRank = -1
        SaveData.isPropts = true
        SaveData.isADmax = 0
        SaveData.LastUpDate = new Date().getDate()
        SaveData.isGuideOver = false
        SaveData.nextPowerTime = 0
        SaveData.power = 10
        SaveData.isGetPower = false
        SaveData.Volume = 10
        SaveData.nextThreePower = 0
        SaveData.shareCelue = {
            strategy: [0, 0, 1, 1, 0, 1, 1, 1, 0, 1],
            num: 0,
            date: 0,
        }
        SaveData.ComboList = [[], []]
        SaveData.ScoreList = [[], []]
        SaveData.PaoPaoLevel = 1

        this.SaveData();
    }
    //    //保存游戏数据
    SaveData() {
        let userData: { [k: string]: any } = {};

        userData.GameData = SaveData.GameData;
        userData.GameDataScore = SaveData.GameDataScore;
        userData.GameDataProp = SaveData.GameDataProp
        userData.isFirstEnter = SaveData.isFirstEnter
        userData.MaxScore = SaveData.MaxScore;
        userData.MaxRank = SaveData.MaxRank;
        userData.isPropts = SaveData.isPropts;
        userData.isADmax = SaveData.isADmax;
        userData.LastUpDate = SaveData.LastUpDate;
        userData.isGuideOver = SaveData.isGuideOver
        userData.power = SaveData.power
        userData.isGetPower = SaveData.isGetPower
        userData.nextPowerTime = SaveData.nextPowerTime;
        userData.Volume = SaveData.Volume;
        userData.nextThreePower = SaveData.nextThreePower
        userData.shareCelue = SaveData.shareCelue
        userData.ComboList = SaveData.ComboList
        userData.ScoreList = SaveData.ScoreList
        userData.PaoPaoLevel = SaveData.PaoPaoLevel

        sys.localStorage.setItem('Water_userData', JSON.stringify(userData));

        // let gameData={
        //     CloudData:cc.sys.localStorage.getItem('Water_userData')
        // }
        // Global.platform.UpdateGameData(gameData)
    }
    //    /**@name: 清除某一条数据 */
    static remove(key) {
        sys.localStorage.removeItem(key);
    }
    //    /**@name: 清除所有数据 */
    static clear() {
        sys.localStorage.clear();
    }
}

